
import {
  queryPromotionPeople,
  queryPromotionPeopleList
} from "../../../../service/jb_promotion_service"
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    menuTop: app.globalData.menuButton.top,
    menuHeight: app.globalData.menuButton.height,
    topSwiperIndex: 0,
    swiperType: 'week',
    requestData: {
      page: 1,
      limit: 20,
      keyword: "",
      grade: 0,
      sortKey: "",
      isAsc: ''
    },
    dataList: [


    ],
    isShowLoading: false,
    isBottomShow: false,
    totalCount: 0,
    refreshFlag: false,
    totalLeftNum: 0,
    totalRightNum: 0,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.queryPromotionPeople();
    this.queryPromotionPeopleList('refresh');
  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  topSwiperChange(e) {
    let index = e.currentTarget.dataset.index;
    this.setData({
      topSwiperIndex: index,
      'requestData.grade': index == 0 ? '0' : "1",
      'requestData.page': 1,
      isBottomShow: false,
    })
    this.queryPromotionPeopleList('refresh')
  },

  searchInputChange(e) {
    this.setData({
      "requestData.keyword": e.detail.value,
    })
  },
  //开始搜索
  handleSearchAction() {
    console.log("JJLog:开始搜索了：" + this.data.requestData.keyword);
    this.setData({
      isBottomShow: false,
      'requestData.page': 1
    })
    this.queryPromotionPeopleList('refresh');
  },

  queryPromotionPeople() {
    wx.showLoading();
    queryPromotionPeople().then(res => {
      wx.hideLoading();
      if (res.code == 200) {
        this.setData({
          totalCount: res.data.total || 0
        })
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      console.error('JJLog:queryPromotionPeople', err);
    })
  },
  queryPromotionPeopleList(type) {
    wx.showLoading();
    queryPromotionPeopleList(this.data.requestData).then(res => {
      wx.hideLoading();
      if (res.code == 200 && res.data) {
        let arr = (type == 'refresh' ? [] : this.data.dataList || []);
        arr = arr.concat(res.data.list || [])
        this.setData({
          dataList: arr,
          refreshFlag: false,
          isShowLoading: false,
          isBottomShow: res.data.totalPage == this.data.requestData.page ? true : false,
        })
        if (this.data.totalLeftNum == 0 && this.data.requestData.grade == 0) {
          this.setData({
            totalLeftNum: res.data.total
          })
        }
        if (this.data.totalRightNum == 0 && this.data.requestData.grade == 1) {
          this.setData({
            totalRightNum: res.data.total
          })
        }
      }
    }).catch(err => {
      wx.hideLoading();
      wx.showToast({
        title: err || '加载失败',
        icon: "none"
      })
      if (this.data.requestData.page != 1) {
        this.setData({
          "requestData.page": this.data.requestData.page - 1
        })
      }
      this.setData({
        refreshFlag: false,
      })
      console.error('JJLog:queryPromotionPeopleList', err);
    })
  },

  //下拉刷新
  handleRefresh() {
    // TODO
    this.setData({
      'requestData.page': 1,
      isBottomShow: false,
      refreshFlag: true,
    })
    this.queryPromotionPeopleList('refresh');
  },
  handleLoadmore() {
    console.log('加载更多');
    if (this.data.isBottomShow) {
      return;
    }
    this.setData({
      isShowLoading: true,
      "requestData.page": this.data.requestData.page + 1
    })
    this.queryPromotionPeopleList()
  },


})